package com.yaiys.thread;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {
    private static  int number=0;
    private static Object object=new Object();
    public static void  addReentrantLock(){
        ReentrantLock reentrantLock=new ReentrantLock(false);//false为非公平锁，true为公平锁
        reentrantLock.lock();
        number++;
        reentrantLock.unlock();

    }
    public static void main(String[] args) {
        long current=System.currentTimeMillis();

        for (int j=0;j<100;j++){
            for (int i=0;i<500000;i++){
                addReentrantLock();
            }

        }
        System.out.println("addReentrantLock最终结果："+number+",使用时间："+(System.currentTimeMillis()-current));
        current=System.currentTimeMillis();
        for (int j=0;j<100;j++){
            for (int i=0;i<500000;i++){
                addSynchronized();
            }
        }
        System.out.println("addSynchronized最终结果："+number+",使用时间："+(System.currentTimeMillis()-current));


    }
    public static void  addSynchronized(){
        synchronized (object){
            number++;
        }
    }
}
